<!-- 订单 -->
<template>
	<view>
		<!-- <u-sticky>
			<view class="order">
				<Screening @select="getSelectDateRange"></Screening>
			</view>
		</u-sticky> -->
		<u-empty v-if="!orderList.length" mode="order" iconSize="240" textSize="40" class="isempty" marginTop="200">
		</u-empty>
		<view class="order" v-else>
			<OrderCard :orderInfo="item" v-for="item in orderList" :key="item['id']" @click="showOrderInfo"
				@Delete="deleteitem"></OrderCard>
		</view>
		<u-back-top :scroll-top="scrollTop" top="200" mode="square" right="5%" duration="500"></u-back-top>
	</view>
</template>
<script>
	import {
		mapGetters
	} from "vuex"
	import OrderCard from './ordercard/ordercard'
	import Screening from 'components/screening'
	import {
		getFormatTime,
		getTimeByDay
	} from "utils/time.js"
	import {
		getMyOrderByPage
	} from "apis/farmer/order.js"
	export default {
		components: {
			OrderCard,
			Screening
		},
		data() {
			return {
				FirstOfDate: false,
				pageNo: 1,
				pageSize: 10,
				//回到顶部
				scrollTop: 0,
				orderList: [],
				startTime: '',
				endTime: ''
			}
		},
		computed: {
			...mapGetters(["getUserLoginState"])
		},
		methods: {
			showOrderInfo() {
				this.pushNewPage("order_state")
			},
			getSelectDateRange(range) {
				this.pageNo = 1
				this.FirstOfDate = true
				this.startTime = range[0]
				this.endTime = range.pop()
			},
			async getData() {
				const {
					code,
					msg,
					data
				} = await getMyOrderByPage(this.pageNo, this.pageSize)
				if (code !== 200) return this.$errorMsg(msg)
				for (const item of data['list']) {
					this.orderList.push(Object.freeze(item))
				}
				this.orderList.sort((a,b)=>new Date(b.createTime) - new Date(a.createTime))
				this.pageNo++

			}
		},

		onShow() {
			if (!this.getUserLoginState) return this.$showCancelModule("用户未登录，是否进行登录",
				() => {
					this.replpacePage("login")
				},
				() => {
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			)

			this.endTime = getFormatTime({
				format: "yyyy-mm-dd"
			})
			this.startTime = getTimeByDay(-7, "yyyy-mm-dd")
			this.getData()
		},
		onPageScroll(e) { //生命周期函数
			this.scrollTop = e.scrollTop
		},
		onReachBottom() { //到达底部
			this.getData();
		},
		async onPullDownRefresh() {
			// 下拉刷新
			this.orderList = []
			this.pageNo = 1
			await this.getData()
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止下拉刷新动画
			}, 500);
		},
	}
</script>

<style>
	@import url("order.css");
</style>
